package net.joyphper.commons.utils;

import java.util.Map;

import javax.mail.internet.MimeMessage;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.mail.javamail.JavaMailSender;
import org.springframework.mail.javamail.JavaMailSenderImpl;
import org.springframework.mail.javamail.MimeMessageHelper;
import org.springframework.ui.freemarker.FreeMarkerTemplateUtils;
import org.springframework.web.servlet.view.freemarker.FreeMarkerConfigurer;
import org.springframework.scheduling.concurrent.ThreadPoolTaskExecutor;
import freemarker.template.Configuration;
import freemarker.template.Template;

public class SimpleMailSender {
	private final Logger logger = Logger.getLogger(this.getClass());

	private JavaMailSender mailSender;
	private String from;
	private String personal;
	
	@Autowired
	private FreeMarkerConfigurer freemarkerConfig;
	
	@Autowired
	private  ThreadPoolTaskExecutor threadPoolTaskExecutor;
	
	public void setMailSender(JavaMailSender mailSender) {
		this.mailSender = mailSender;
	}

	public void setFrom(String from) {
		this.from = from;
	}

	public void setPersonal(String personal) {
		this.personal = personal;
	}
	/**
	 * 发送邮件，不带附件
	 * @param to 接收人地址
	 * @param subject 主件主题
	 * @param body	主件内容
	 * @return
	 */
	public boolean send(String to, String subject, String body) {
		try {
			JavaMailSenderImpl senderImpl = new JavaMailSenderImpl();

			MimeMessage mailMessage = senderImpl.createMimeMessage();

			// 设置utf-8或GBK编码，否则邮件会有乱码
			MimeMessageHelper messageHelper = new MimeMessageHelper(mailMessage, true, "utf-8");

			messageHelper.setTo(to);// 接受者

			messageHelper.setFrom(this.from, this.personal);// 发送者

			messageHelper.setSubject(subject);// 主题

			// 邮件内容，注意加参数true，表示启用html格式
			messageHelper.setText(body, true);

			//this.mailSender.send(mailMessage);
			
			//启用线程的方式发送邮件，邮件发送成功与失败不影响到系统和执行结果
			this.threadPoolTaskExecutor.execute(new MailRunnable(mailMessage,this.mailSender));

		} catch (Exception e) {
			logger.error("发送HTML邮件失败", e);
			return false;
		}
		
		return true;
	}
	
	/**
	 * 发送模板邮件
	 * @param to	接收人地址
	 * @param subject	邮件主题
	 * @param tpl	freemarker模板
	 * @param map	向freemarker模板传递的值
	 * @return
	 */
	public boolean send(String to, String subject, String tpl,Map<String,Object> map) {
		try {
			
			JavaMailSenderImpl senderImpl = new JavaMailSenderImpl();

			MimeMessage mailMessage = senderImpl.createMimeMessage();

			// 设置utf-8或GBK编码，否则邮件会有乱码
			MimeMessageHelper messageHelper = new MimeMessageHelper(mailMessage, true, "utf-8");

			messageHelper.setTo(to);// 接受者

			messageHelper.setFrom(this.from, this.personal);// 发送者

			messageHelper.setSubject(subject);// 主题

			/*---------构建邮件内容-----------*/
			 Configuration configuration = this.freemarkerConfig.getConfiguration();
			 Template template = configuration.getTemplate(tpl);
			 String bodyText = FreeMarkerTemplateUtils.processTemplateIntoString(template , map);
			 
			// 邮件内容，注意加参数true，表示启用html格式
			messageHelper.setText(bodyText, true);
			
			this.threadPoolTaskExecutor.execute(new MailRunnable(mailMessage,this.mailSender));

		} catch (Exception e) {
			logger.error("发送HTML邮件失败", e);
			return false;
		}
		
		return true;
	}
}

class MailRunnable implements Runnable{
	private JavaMailSender mailSender;
	private MimeMessage mailMessage;
	public MailRunnable(MimeMessage mailMessage,JavaMailSender mailSender) {
		this.mailSender=mailSender;
		this.mailMessage = mailMessage;
	}
	
	@Override
	public void run() {
		this.mailSender.send(this.mailMessage);
	}
}
